define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    
    $.widget('rk.pageOAMenuCtrl', {
            _create: function(){
                var me = this;
                var elem = me.element;            
            },
            close_timer: null,
            _init: function(){
                var me = this;
                var elem = me.element;
                var opt = me.options;
                var button = elem.find('a.dropdown-toggle');

                var itemsTpl = require('../../../tmpl/frame/oa_menu.tpl');
                var itemsHtml = rk.templateText(itemsTpl, {items: me._getItems()});

                elem.append(itemsHtml);

                button.rkEasyMenu({
                    animate: true,
                    menuId: 'oamenu',
                    openBy: 'mouseover',
                    onSelect: function(li){
                        me._handleClick(li.find('>a[act]'))
                    },
                    onOpen: function(){
                        if(opt.onSysOpen)(opt.onSysOpen)();
                    }
                });
            },
            _getItems: function(){
                var items = window.SESSION.oaMenuItems;
                var resultItems = [];
                for(var i = 0, len = items.length;i<len;i++){
                    var item = items[i];
                    if(item.name !== "privateMessage"){
                        resultItems.push({
                            name: item.name,
                            title: (item.name != 'announcement' ? rk.i18n('#'+item.name+'#') : rk.i18n('TITLE_ANNOUNCEMENT'))
                            //title: rk.i18n('TITLE_'+item.name.toUpperCase())
                        });
                    }
                }
                return resultItems;
            },
            openMenu: function(){
                var me = this;
                var elem = me.element;
                var opt = me.options;
                var button = elem.find('a.dropdown-toggle');
                button.rkEasyMenu('open');
            },
            closeMenu: function(){
                $('#oamenu').hide();
            },
            _handleClick: function(a){
                var me = this;
                var elem = me.element;
                var act = a.attr('act');

                me.click(act);
            },
            click:function(act, opt, ctrlOpt){
                var me = this;
                var elem = me.element;
                var opt = me.options;

                if(typeof opt == 'undefined' || !opt) opt = {};
                if(typeof ctrlOpt == 'undefined') ctrlOpt = {};
				
				//if(window._hmt) window._hmt.push(['_trackEvent', 'oa_menu', act, window.SESSION.tenantName]);

                require.async([
                                'oa/js/'+act+'/'+act+'PanelCtrl'//,
                                // 'oa/js/'+act+'/'+act+'CurtainCtrl',
                                // 'oa/js/'+act+'/'+act+'ContentCtrl'                                
                            ], function(al, pg) {
                                var fullname = rk.getFullAppName(act);
                                var rsName = 'oa_' + fullname;//资源名，在/resources/sub-deps/中定义
                                    rk.oa.popupPanel({
                                        act: act,
                                        onSuccess: function(panelSel){
                                            var panel = $(panelSel);
                                            var ctrl = act + 'PanelCtrl';
                                            if(panel[ctrl]){
                                                (panel[ctrl])(ctrlOpt);
                                            }
                                            if(opt.onSysPanelOpen)(opt.onSysPanelOpen)();
                                            me.closeMenu();
                                        }
                                    });
                            });
            },
            showClosingIcon: function(){
                var me = this;
                var elem = me.element;
                var oaTogger = $('#oa_panel_toggler');
                oaTogger.find('span').addClass('oa-slidebar-taggle').removeClass('oa-slidebar-taggle-open');
                oaTogger.beTip({title: rk.i18n('PAGE_OPEN_OA_PANEL'), dir: 'left'});
            },
            showOpeningIcon: function(){
                var me = this;
                var elem = me.element;
                var oaTogger = $('#oa_panel_toggler');
                oaTogger.find('span').addClass('oa-slidebar-taggle').addClass('oa-slidebar-taggle-open');
                oaTogger.beTip({title: rk.i18n('PAGE_CLOSE_OA_PANEL'), dir: 'left'});
            }
    });
});